<?php


class App_Models_Photo
{
    public function get_albums_photos($albums_id)
    {
        $db = Core_Db::instance();
        $result_photos = $db->query("SELECT * FROM `photos`  WHERE `albums_id` = " .  mysql_real_escape_string($albums_id) . ";");

        if ($result_photos) {
          
            $result_albums = $db->query("SELECT `name` FROM `albums` WHERE  `id`='".mysql_escape_string($albums_id)."';");
            if ($result_albums) {
                $albums_name = $result_albums[0]['name'];

                foreach ($result_photos as $key => $value) {
                    if (is_file(APP . 'Views' . DS . 'albums' . DS . 'img' . DS . $albums_name . DS . $value['file_name'])) {
                        $result_photos[$key]['img'] = '/App/Views/albums/img/' . DS . $albums_name . DS . $value['file_name'];
                    } else {
                        $result_photos[$key]['img'] = '/App/Views/albums/img/default_albums.jpg';
                    }
                }
                return $result_photos;
           } else {
            return false;
           }
        } else {
            return false;
        }
    }

    public function get_random_foto()
    {
        $db = Core_Db::instance();

        $photo = $db->query("SELECT * FROM `photos`;");

        if ($photo) {
            $rand_key = array_rand($photo, 6);
                foreach ($rand_key as $key){
                   $return[] = $photo[$key];
                }
            return $return;
        } else {
            return false;
        }

    }

    public function get_albums_photo($photo_id)
    {
        $db = Core_Db::instance();

        $photo = $db->query("SELECT * FROM `photos` WHERE `id`='". mysql_real_escape_string($photo_id)."';");

        if ($photo) {
            $albums = $db->query("SELECT * FROM `albums` WHERE `id`='". mysql_real_escape_string($photo[0]['albums_id'])."';");
            $result[0] = $photo[0];
            $result[1] = $albums[0];
            return $result;
        } else {
            return false;
        }
    }

    public function get_index($al_photo,$photo_id)
    {
        foreach ($al_photo as $key => $value){
            if ($photo_id === $value['id']) {
                return $key;
            }
        }
    }

    public function set_index($count,$current_index,&$prev,&$next)
    {

        if (0 === $current_index) {
            if (1 === $count) {
                $next = 0;
                $prev = 0;
            } else {
                $next = 1;
                $prev = $count-1;
            }
        } else {
            if (($count -1) === $current_index) {
                $next = 0;
                $prev = $current_index - 1;
            } else {
                $prev = $current_index - 1;
                $next = $current_index + 1;
            }
        }

    }

}

?>